﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Infragistics.Win.AppStyling;
using QLNHK.BLL;
using System.Data.SqlClient;

namespace QLNHK
{
    static class Program
    {
        public static frmMain formMain;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                StyleManager.Load(Application.StartupPath + "\\" + "Styles\\Office2007Blue.isl", true);

                //CHECK CONNECT DB
                Global.DB_CONNECTION = Global.ReadNodeXmlDBConnectionStrings("MSSQL");
                bool valid = false;
                try
                {
                    //Kiem tra ket noi den SERVER
                    valid = SQL.TestConnection(string.Format("Server={0};Database={1};Uid={2};Pwd={3}", new object[] { Global.DB_CONNECTION.Server, "master", Global.DB_CONNECTION.User, Global.DB_CONNECTION.Pass }));

                    //Kiem tra xem Database da duoc tao chua
                    if (valid)
                        valid = SQL.TestConnection(string.Format("Server={0};Database={1};Uid={2};Pwd={3}", new object[] { Global.DB_CONNECTION.Server, Global.DB_CONNECTION.Database, Global.DB_CONNECTION.User, Global.DB_CONNECTION.Pass }));

                    if (!valid)
                    {
                        valid = SQL.AttachDatabase(Global.DB_CONNECTION.Server, Global.DB_CONNECTION.Database, Global.DB_CONNECTION.User, Global.DB_CONNECTION.Pass);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                formMain = new frmMain();
                Application.Run(formMain);
            }
            catch (SqlException exsql)
            {
                DialogResult result = MessageBox.Show("Server không tồn tại. Yêu cầu thiết lập lại cấu hình máy chủ.\nChọn Yes (Đồng ý) để thiết lập lại, No (Từ chối) đê thoát khỏi chương trình.", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    frmCauHinh f = new frmCauHinh();
                    if (f.ShowDialog() == DialogResult.OK)
                        Application.Run(new frmMain());
                    else
                        Application.Exit();
                }
                else
                {
                    Application.Exit();
                }
                Logger.LocalLogger.Instance().WriteMessage(exsql);
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
                //MessageBox.Show(string.Format(ThongBao.APPLICATION_ERROR_03Param, GlobalSettings.NGUOILIENHEHOTRO, GlobalSettings.DIENTHOAIHOTRO, GlobalSettings.EMAILHOTRO), false, true, ex);
                Application.Exit();
            }
        }
    }
}
